from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import AlreadySer,ReserveSer
from url.send_sms_code import send_message
import re,random,redis
# Create your views here.

# 预定的所有数据
class ReserveView(APIView):
    def get(self,request):
        reserve = Reserve.objects.all()
        print('dasfas',reserve)
        res = ReserveSer(reserve,many=True)
        return Response(res.data)

    def post(self,request):
        # 接受前端数据
        name = request.data.get('name')
        value3 = request.data.get('value3')
        value4 = request.data.get('value4')
        password = request.data.get('password')
        password1 = request.data.get('password1')

        user = Users.objects.get(user_name=name)
        ret = user.id
        huiyi = user.meeting

        Reserve.objects.create(
            name_id=ret,
            start_time=value3,
            finish_time=value4,
            mem_pad=password,
            text=password1,
            huiyihao=huiyi,
            title='打扫的撒辅导辅导是否是',
            proceed='待开始'
        )
        return Response({
            'code':201,
            'msg':'添加成功'
        })





# 已有的所有数据
class AlreadyView(APIView):
    def get(self, request):
        reserve = Already.objects.all()

        res = AlreadySer(reserve, many=True)

        return Response(res.data)

    def post(self,request):
        # 接受前端数据
        huiyi = request.data.get('huiyi')
        name = request.data.get('name')
        print('dasfa',huiyi,name)

        try:
            user = Users.objects.get(user_name=name)
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':500,
                'msg':'没有此用户'
            })

        u_id = user.id

        if Already.objects.filter(name_id=u_id,huiyihao=huiyi):


        # res = AlreadySer(alr, many=True)

            return Response({
                'code':200,
                'msg':'进会成功'
            })
        return Response({
            'code':500,
            'msg':'没有此会议号'
        })

class Sjy(APIView):
    def get(self,request):
        phone=request.query_params.get("phone")
        if not re.findall(r'^1[3-9]\d{9}$',phone):
            return Response({"code":400,'msg':'手机号不符合'})
        resp=send_message.delay(phone)
        print('****',resp)
        return Response({"code":200,'msg':'获取成功'})


# 注册/登录
from day0822_django.settings import SECRET_KEY
import jwt
def g_jwt(user):
    lst={
        'id':user.id,
        'phone':user.phone
    }
    token=jwt.encode(payload=lst,algorithm="HS256",key=SECRET_KEY)
    return token
class Deng(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        yzm=request.data.get('yzm')
        if not all([phone,yzm]):
            return Response({"code":400,'msg':'字段不完整'})
        if not re.findall(r'^1[3-9]\d{9}$',phone):
            return Response({'code':400,'msg':'手机号不符合'})
        r=redis.Redis(host='127.0.0.1',port=6379,db=1)
        c1=r.get(f'sms_code_{phone}')
        if c1:
            c1=c1.decode()
        else:
            return Response({'code':401,'msg':'验证码已过期'})
        if str(c1) !=str(yzm):
            return Response({'code':402,'msg':'验证码错误'})

        try:
            aa=Mdeng.objects.get(phone)
            token=g_jwt(aa)
            return Response({"code": 200, 'msg': '登录成功', 'token': token, 'user': {
                'id': aa.id,
                'phone': aa.phone
            }})
        except Exception as  a:
            bb=Mdeng.objects.create(phone=phone)
            token=g_jwt(bb)
            return Response({"code": 200, 'msg': '登录成功', 'token': token, 'user': {
                'id': bb.id,
                'phone': bb.phone
            }})

